package com.jiker;

import com.jiker.order.*;

import java.util.*;

/**
 * @author leo
 * @date 2021/6/16
 */
public class Controller {
    private final Area area;
    private Coordinates coordinates;

    public Controller(Coordinates coordinates, Area area) {
        this.coordinates = coordinates;
        this.area = area;
    }

    public Coordinates sendMessages(String command) {
        ArrayList<Order> list = new ArrayList<>();
        Arrays.stream(command.split("")).forEach(element -> {this.handleOrders(element,list);});
        return controller(list);
    }

    public Coordinates sendMessages(String command,List<Barrier> barriers) {
        ArrayList<Order> list = new ArrayList<>();
        Arrays.stream(command.split("")).forEach(element -> {this.handleOrders(element,list);});
        return controller(list,barriers);
    }

    private void handleOrders(String element,ArrayList<Order> list) {
        if ("f".equals(element)) {
            list.add(new MoveForward(area));
        }
        if ("b".equals(element)) {
            list.add(new MoveBack(area));
        }
        if ("l".equals(element)) {
            list.add(new TurnLeft(area));
        }
        if ("r".equals(element)) {
            list.add(new TurnRight(area));
        }
    }

    private Coordinates controller(List<Order> orders) {
        MarsRover marsRover = new MarsRover();
        marsRover.setCoordinates(coordinates);
        return marsRover.run(orders);
    }

    private Coordinates controller(List<Order> orders, List<Barrier> barriers) {
        MarsRover marsRover = new MarsRover();
        marsRover.setCoordinates(coordinates);
        return marsRover.run(orders,barriers);
    }
}